var express = require('express');
var router = express.Router();
const shotService = require("../service/shotService");

router.get('/', async function (req, res, next) {
    let { type, value, current, pageSize } = req.query;
    let params = {};

    if (type) {
        params[type] = value;
    }

    let data = await shotService.find(params, {
        current: current || 1,
        pageSize: pageSize || 5
    })

    res.send({
        data
    })
});

/**
 * @swagger
 * /shot/all:
 *    get:
 *      tags:
 *      - shot
 *      summary: 查找全部
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
router.get("/all", async function (req, res, next) {
    let data = await shotService.findAll();
    res.send({
        data
    })
})


/**
 * @swagger
 * /shot:
 *    post:
 *      tags:
 *      - shot
 *      summary: 添加
 *      requestBody:
 *        required: true
 *        content:
 *          application/x-www-form-urlencoded:
 *            schema:
 *              type: object
 *              properties:
 *                name:
 *                  type: string
 *                price:
 *                  type: string
 *                freight:
 *                  type: string
 *                count:
 *                  type: number
 *                color:
 *                  type: string
 *                fit:
 *                  type: string
 *                brand:
 *                  type: string
 *                size:
 *                  type: string
 *                types:
 *                  type: string
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
router.post("/", async function (req, res, next) {
    let params = req.body;
    // console.log(params);
    // let arr = [];
    // arr.push(params)
    console.log(params);
    let result = await shotService.insert(params);
    res.send(result);
})

router.get("/:id", async function (req, res, next) {
    let id = req.params.id;
    let data = await shotService.findById(id);
    res.send({
        data
    })
})

router.put("/:id", async function (req, res, next) {
    let id = req.params.id;
    let params = req.body;

    let result = await shotService.update({ id, ...params });
    res.send({
        result
    })
})

router.delete("/:id", async function (req, res, next) {
    let id = req.params.id;

    let result = await shotService.remove(id);
    res.send({
        result
    })
})

module.exports = router;